# -*- coding: utf-8 -*-
"""
Created on 2016年2月15日
@author: yytan 77890
"""
from pyse.mail import config_mail as CFGMAIL
import smtplib
from email.mime.text import MIMEText


def send_mail(msgtext):
    msg = MIMEText('%s' %msgtext, 'html', 'utf-8')
    msg['Subject'] = CFGMAIL.subject
    # 初始化
    smtp = smtplib.SMTP()
    # 持续尝试发送，直到发送成功
    while 1:
        try:
            # 发送邮件
            smtp.sendmail(CFGMAIL.sender, CFGMAIL.receiver, msg.as_string())
            smtp.quit()
            print('邮箱配置成功')
            break
        except:
            try:
                # 连接至邮件服务器
                smtp.connect(CFGMAIL.smtpserver)
                # 登录邮件服务器
                smtp.login(CFGMAIL.user, CFGMAIL.password)
            except:
                # 登录失败
                print('failed to login to smtp server')

if __name__ == "__main__":
    MSG = '<html>This is a test mail.<br>Congratulations! Mail config success.</html>'
    send_mail(MSG)
